//
//  AdvertPage.m
//  NewsReader
//
//  Created by Rain on 16/7/22.
//  Copyright © 2016年 MyCos. All rights reserved.
//

#import "AdvertPage.h"
#import "AppDelegate.h"
#import "FxDate.h"
#import "FxAppSetting.h"
#import "FxGlobal.h"
#import "FxGetImage.h"

@implementation AdvertPage

//是否显示广告页面
+(BOOL)canShowAdvertPage
{
    //取出来显示广告的时间,在showAdvertPage中纪录了时间
    NSString *dateString = [FxAppSetting getValue:AdvertKey];
    
    NSDate *date = [FxDate dateFromStringYMDHMS:dateString];
    
    //纪录上一次广告显示时间
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    
    //1小时内不显示
    if (interval < AdvertCheckTime) {
        return NO;
    }
    
    return YES;
}

+(void)showAdvertPage
{
    //显示广告的时候把显示广告的时间存起来，方便canShowAdvertPage方法判断多长时间后再显示广告
    [FxAppSetting setValue:[FxDate stringFromDateYMDHMS:[NSDate date]] forKey:AdvertKey];
    
    AdvertPage *controller = [[AdvertPage alloc]init];
    
    //不知道这是干什么
    UIWindow *window = [AppDelegate appDeg].window;
    
    if (window.rootViewController != nil) {
        CGRect frame = window.rootViewController.view.bounds;
        
        controller.view.frame = frame;
        
        [window.rootViewController.view addSubview:controller.view];
    }else{
        window.rootViewController=controller;
        
        [window makeKeyAndVisible];
    }
}

#pragma mark - 生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self getAdvertImage];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

#pragma mark -
-(void)getAdvertImage
{
    //如果本定存在，就显示本地的
    if ([self checkLaunchAdverImage]) {
        return;
    }
    
    [self getAdvertOp];
}

//从网络上获取图片
-(void)getAdvertOp
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    
    NSString *url = [NSString stringWithFormat:AdvertURL,(long)size.width,(long)size.height];
    
    NSDictionary *dictInfo = @{@"url":url};
    
    _operation = [[FxOperation alloc] initWithDelegate:self opInfo:dictInfo];
    
    [_operation executeOp];
    
}

//有广告图片的url，获取(下载)这张图片的数据
-(void)getLaunchImageOp:(NSString *)url
{
    NSDictionary *dictInfo = @{@"url":url};
    
    _operation = [[FxGetImage alloc] initWithDelegate:self opInfo:dictInfo];
    
    [_operation executeOp];
}

#pragma mark - 检测是否有本地广告图，如果有就显示
-(BOOL)checkLaunchAdverImage
{
    
    NSString *fileName = [FxDate stringFromDateYMD:[NSDate date]];
    
    NSString *filePath = [FxGlobal getCacheImage:fileName];
     
    if ([FxFileUtility isFileExist:filePath]) {
        _imageView .image = [UIImage imageWithContentsOfFile:filePath];
        
        //延迟几秒再销毁
        [self delayHideAdevert];
        
        return YES;
    }
    
    return NO;
}

-(void)delayHideAdevert
{
    [self performSelector:@selector(hideLanch) withObject:nil afterDelay:AdvertDelayTime];
}

//广告图延迟几秒后，删除
-(void)hideLanch
{
    //如果当前是一个window就直接删除，否则显示homepage页面
    if (self.view.superview != [AppDelegate appDeg].window) {
        [self.view removeFromSuperview];
    }
    else
        [[AppDelegate appDeg] showHomePage];
}

#pragma mark - FxOperationDelegate methods
-(void)opSuccess:(NSDictionary *)dict
{
    NSDictionary *dictData = [dict objectForKey:NetData];
    
    NSString *url = [dictData objectForKey:@"imageUrl"];
    
    [self getLaunchImageOp:url];
}

//getLaunchImageOp方法把广告图片数据下载后的回调
-(void)opSuccessEx:(id)data opinfo:(NSDictionary *)dictInfo
{
    [self setLacuchImage:data];
    
    [self delayHideAdevert];
}

//把图片缓存到本地
-(void)setLacuchImage:(NSData *)data
{
    NSString *fileName = [FxDate stringFromDateYMD:[NSDate date]];
    
    UIImage *image = [UIImage imageWithData:data];
    
    if (image != nil) {
        _imageView.image = image;
        
        [data writeToFile:[FxGlobal getCacheImage:fileName] atomically:YES];
    }
}

@end
